// pages/login/login.js
var api = require("../../config/api.js")
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        username: '',
        password: '',
        sms: '',
        phone: '',
        code:''
    },
    onClickLogin_sms: function () {
        console.log(this.data),
        wx.request({
            
            url: api.LoginAPI,
            data: {
                phone: this.data.phone,
            },
            dataType: 'json',
            method: 'GET',
            responseType: 'json',
            success: (result) => {
                console.log('验证码发送成功')
                console.log(result)
                this.setData({
                    code: result.data.code

                })
            },
            fail: (res) => { },
            complete: (res) => { },
        })
    },

    onClickLogin_a(options) {
        // var app_a = this.app
        // console.log(this.data)login.js? [sm]:13 {username: "常雷", password: "151315145114", __webviewId__: 219}
        if (0 < this.data.username.length < 20 & 6 < this.data.password.length < 128) {
            console.log(this.data)
            wx.login({
                success: (result) => {
                    console.log(result.code)
                    wx.request({
                        url: api.LoginAPI,
                        data: {
                            username: this.data.username,
                            password: this.data.password,
                            wx_code: result.code
                        },
                        dataType: 'json',
                        method: 'POST',
                        responseType: 'text',
                        success: (result) => {
                            console.log('账号登录成功')
                            console.log(result.data)
                            app.globalData.is_active= result.data.is_active,
                            app.globalData.phone=result.data.phone,
                            app.globalData.token= result.data.token,
                            app.globalData.avatar= result.data.avatar,
                            app.globalData.username=this.data.username,
                            app.globalData.password=this.data.password,
                            wx.setStorageSync('Logintokenkey', result.tokent)
                            console.log(app.globalData)
                            this.setData({
                                avatar: result.data.avatar,
                            })
                            wx.switchTab({
                              url: '../home/home',
                            })
                        },
                        fail: (res) => { },
                        complete: (res) => { },
                    })
                },
                fail: (res) => { },
                complete: (res) => { },
            })
        } else {
            wx.showToast({
                title: '输入格式错误',
                icon: 'error',
            })
        }

    },
    onClickLogin_b() {
        // (/^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/)
        if ((/^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/.test(this.data.phone))){
            console.log(this.data)
            wx.login({
                success: (result) => {
                    console.log(result.code)
                    wx.request({
                        url: api.LoginAPI,
                        data: {
                            phone: this.data.phone,
                            sms:this.data.sms,
                            wx_code: result.code,
                            code:this.data.code,
                        },
                        dataType: 'json',
                        method: 'POST',
                        responseType: 'text',
                        success: (result) => {
                            console.log('手机号登录成功')
                            console.log(result.data)
                            app.globalData.is_active= result.data.is_active,
                            app.globalData.username= result.data.username,
                            app.globalData.phone= this.data.phone,
                            app.globalData.token= result,
                            wx.setStorageSync('Logintokenkey', result.tokent)
                            wx.switchTab({
                              url: '../../pages/home/home',
                            })
                        },
                        fail: (res) => { },
                        complete: (res) => { },
                    })
                },
                fail: (res) => { },
                complete: (res) => { },
            })
        } else {
            wx.showToast({
                title: '手机号输入格式错误',
                icon: 'error',
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})